package com.example.fwcz.controller.admin;

import com.example.fwcz.entity.meetInfo;
import com.example.fwcz.entity.user;
import com.example.fwcz.model.vo.Result;
import com.example.fwcz.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class UserAdminController {
    @Autowired
    UserService userService;

    /**
     * 注册用户
     * @param user
     * @return
     */
    @PostMapping("/useradd")
    public Result addUser(@RequestBody user user){
        userService.addUser(user);
        return Result.ok("用户添加成功");
    }

    /**
     * 删除用户
     * @param username
     * @return
     */
    @DeleteMapping("/userdelete")
    public Result deleteUser(@RequestParam String username){
        userService.deleteUser(username);
        return Result.ok("用户删除成功");
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/userchange")
    public Result changeUser(@RequestBody user user){
        userService.changeUser(user);
        return Result.ok("用户信息修改成功");
    }

    /**
     * 搜索所有用户
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/userall")
    public Result searchAlluser(@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize){
        String orderBy="id desc";
        PageHelper.startPage(pageNum, pageSize, orderBy);
        PageInfo<user> pageInfo=new PageInfo<>(userService.searchAlluser());
        return Result.ok("请求成功",pageInfo);
    }

    /**
     * 根据【用户名】查询用户信息
     * @param username
     * @return
     */
    @GetMapping("/userone")
    public Result searchOneuser(@RequestParam String username){
        return Result.ok("请求成功",userService.searchOneuser(username));
    }
}
